package net.xdclass.onlinexdclass.exception;

import net.xdclass.onlinexdclass.utils.JsonDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 */
@RestControllerAdvice
public class CustomExceptionHandler {


    private final static Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    public JsonDate handler(Exception e) {

        logger.error("[系统异常]{}", e);

        if (e instanceof XDException) {

            XDException xdException = (XDException) e;
            return JsonDate.bulidError(xdException.getMessage(),xdException.getCode());

        } else {
            return JsonDate.bulidError("全局异常，未知错误");

        }

    }
}
